package hibernate;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import bean.MessageBean;
import bean.MessagePriveBean;
import bean.PublicationBean;

public class MessageRepository extends GenericRepository<MessageBean, Integer> implements Serializable {

	private static final long serialVersionUID = 1L;

	public MessageBean GetById(Integer Id){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(MessageBean.class).add(Restrictions.eq("id_message", Id));
		MessageBean msg = (MessageBean) criteria.uniqueResult();
		trans.commit();
		return msg;
	}

	public List<MessageBean> GetAll(){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(MessageBean.class);
		@SuppressWarnings("unchecked")
		List<MessageBean> lst = (List<MessageBean>) criteria.list();
		trans.commit();
		return lst ;
	}

	public List<MessagePriveBean> GetAllMessagePriveByDestinataire(int id_destinataire){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(MessagePriveBean.class).add(Restrictions.eq("id_eleve_destinataire", id_destinataire)).add(Restrictions.eq("privacy", true));
		@SuppressWarnings("unchecked")
		List<MessagePriveBean> lst = (List<MessagePriveBean>) criteria.list();
		trans.commit();
		return lst ;
	}

	public List<PublicationBean> GetAllPublicationByDestinataire(int id_destinataire){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(PublicationBean.class).add(Restrictions.eq("id_eleve_destinataire", id_destinataire)).add(Restrictions.eq("privacy", false));
		@SuppressWarnings("unchecked")
		List<PublicationBean> lst = (List<PublicationBean>) criteria.list();
		trans.commit();
		return lst ;
	}
}
